package com.aaa.hospital.Configurationterminal.config;



import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;







@Controller
@Configuration
@EnableWebSecurity  //开启security功能
public class springsecunity extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();

        //首页都可以访问
        http.authorizeRequests().antMatchers("/").permitAll()

        .antMatchers("/chap1/**").hasRole("vip1")
        .antMatchers("/chap2/**").hasRole("vip2")
        .antMatchers("/chap3/**").hasRole("vip3");
        //没有权限时跳转到登录页面
        http.formLogin();
    }

    @RequestMapping("/test3")
    public String test(){
        return "";
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2")
                .and().withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip2")
                .and().withUser("user").password(new BCryptPasswordEncoder().encode("user")).roles("vip3")
        ;
    }
}
